package com.hc.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.apache.log4j.Logger;
/**
 * 日期工具类
* @ClassName: DateUtil
* @author 丁明华
* @date 2012-12-5 下午01:58:06
*
 */
public class DateUtil {
	private static Logger logger = Logger.getLogger(DateUtil.class);
//	public static final String DEFAULT_STRING = "yyyy-MM-dd HH:mm:ss";
	public static final String DEFAULT_STRING = "yyyy-MM-dd HH:mm:ss";
	public static final String FORMAT_STRING_0 = "yyyy/MM/dd HH:mm:ss";
	public static final String FORMAT_STRING3 = "yyyy/MM/dd";
	public static final String FORMAT_DAY = "yyyy-MM-dd";
	public static final String FORMAT_TIME = "HH:mm:ss";
	
	 /**
	  *   时间戳转换为字符串
	 * @Title: timeToString
	 * @param time 时间戳
	 * @return  String对象
	  */
	public static String timeToString(Timestamp time){
		if(time == null){
			return "";
		}
		SimpleDateFormat df = new SimpleDateFormat(DEFAULT_STRING);//定义格式，不显示毫秒
		return df.format(time);
	}
	 /**
	  *   字符串转换时间戳
	 * @Title: stringToTime
	 * @param str 字符串
	 * @param format 日期格式
	 * @return  Timestamp对象
	  */
	public static Timestamp stringToTime(String time){
		return Timestamp.valueOf(time);
	}
	
	/**
	 *  获取当前时间，以字符串型显示
	* @Title: getNowTimeString
	* @return  String对象
	* @deprecated: 已经移植到DateUtil
	 */
	public static String getNowTimeString() {
		SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_STRING);
		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08"));
		return dateFormat.format(new Date());
	}
	
	public static String getNowTimeString(String strFormat) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08"));
		return dateFormat.format(new Date());
	}
	
	/**
	 * 获取一个指定日期格式的当前时间，以时间戳显示
	* @Title: getNowTimestamp
	* @param strFormat 日期格式
	* @return  Timestamp对象
	 */
	public static Timestamp getNowTimestamp(String strFormat) {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08"));
		String now = dateFormat.format(cal.getTime());
		return Timestamp.valueOf(now);
	}
	
	/**
	 * 字符串转换成日期 如果转换格式为空，则利用默认格式进行转换操作
	* @Title: str2Date
	* @param str 字符串
	* @param strFormat 日期格式
	* @return Date对象
	* @throws java.text.ParseException
	 */
	public static Date str2Date(String str, String format) {
		if (null == str || "".equals(str)) {
			return null;
		}
		// 如果没有指定字符串转换的格式，则用默认格式进行转换
		if (null == format || "".equals(format)) {
			format = DEFAULT_STRING;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = sdf.parse(str);
			return date;
		} catch (ParseException e) {
			logger.error("Exception", e);
		}
		return null;
	}

	/**
	 * 日期转换为字符串
	* @Title: date2Str
	* @param date 日期
	* @param format 日期格式
	* @return String对象
	* @throws java.text.ParseException
	 */
	 public static String date2Str(Date date, String format) {
	  if (null == date) {
	   return null;
	  }
	  SimpleDateFormat sdf = new SimpleDateFormat(format);
	  return sdf.format(date);
	 }

	 /**
	  *   时间戳转换为字符串
	 * @Title: timestamp2Str
	 * @param time 时间戳
	 * @param format 日期格式
	 * @return  String对象
	  */
	 public static String timestamp2Str(Timestamp time, String format) {
		if (null == time) {
			return "";
		}
		// 如果没有指定字符串转换的格式，则用默认格式进行转换
		if (null == format || "".equals(format)) {
			format = DEFAULT_STRING;
		}
		Date date = null;
		if (null != time) {
			date = new Date(time.getTime());
		}
		return date2Str(date, format);
	}

	 /**
	  *  字符串转换时间戳
	 * @Title: str2Timestamp
	 * @param str 字符串
	 * @param format 日期格式
	 * @return  Timestamp对象
	  */
	 public static Timestamp str2Timestamp(String str, String format) {
		// 如果没有指定字符串转换的格式，则用默认格式进行转换
		if (null == format || "".equals(format)) {
			format = DEFAULT_STRING;
		}
		Date date = str2Date(str, format);
		return new Timestamp(date.getTime());
	}
	 
	 /**
		 *  日期转换为字符串
		* @Title: calendar2Str
		* @param calendar 日历
		* @param strFormat 日期格式
		* @return String对象
		 */
	public static String calendar2Str(Calendar calendar, String strFormat) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
		return dateFormat.format(calendar.getTime());
	}
	/**
	 * 字符串转换为格式
	* @Title: str2Calendar
	* @param time 字符串型时间
	* @param strFormat 日历格式
	* @return Calendar对象
	 */
	public static Calendar str2Calendar(String time, String strFormat) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(str2Date(time, strFormat));
		return calendar;
	}
	
	/**
	 * 获取明天
	* @Title: getTomorrowTime
	* @return  Date对象
	 */
	public static Date getTomorrowTime() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		return cal.getTime();
	}
	
	/**
	 * 得到某一时间范围的给定时间
	 * @param time
	 * @param testTime
	 * @return
	 */
	
	public static String getNextTime(String time,int testTime,String strFormat) {
		Timestamp stamp = DateUtil
				.str2Timestamp(time, strFormat);
		stamp.setMinutes(stamp.getMinutes() + testTime);
		return DateUtil.timestamp2Str(stamp, strFormat);
	}
	
	public static Date getTodayTime() {
		Calendar cal = Calendar.getInstance();	
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		return cal.getTime();
	}
	
	public static boolean isCurrentWeek(String date,String format) {
		Calendar temp = str2Calendar(date, format);
		Calendar current = Calendar.getInstance();
		current.setTime(new Date());
		current.setFirstDayOfWeek(Calendar.MONDAY);
		temp.setFirstDayOfWeek(Calendar.MONDAY);
		if (temp.get(Calendar.YEAR) != current.get(Calendar.YEAR)) {
			return false;
		}
		return temp.get(Calendar.WEEK_OF_YEAR) == current.get(Calendar.WEEK_OF_YEAR);
	}
	
	public static boolean iscurrentMonth(Calendar date) {
		Calendar current = getNowTime();
		if (date.get(Calendar.YEAR) != current.get(Calendar.YEAR)) {
			return false;
		}
		return date.get(Calendar.MONTH) == current.get(Calendar.MONTH);
	}
	
	public static boolean iscurrentMonth(String date) {
		return iscurrentMonth(str2Calendar(date, DEFAULT_STRING));
	}
	
	public static Calendar getNowTime() {
		Calendar current = Calendar.getInstance();
		current.setTime(new Date());
		return current;
	}
	
	public static int currentMonth() {
		return month(new Date());
	}
	
	public static int month(Date date) {
		Calendar current = Calendar.getInstance();
		current.setTime(date);
		return current.get(Calendar.MONTH) + 1;
	}
	
	public static int month(String date) {
		return month(DateUtil.str2Date(date, DateUtil.DEFAULT_STRING));
	}
	
	
	
	
	
	
	public static String getCurrentTimeBeforeTime(String time,String format) {
		Calendar temp = DateUtil.str2Calendar(time,format);
		temp.set(Calendar.HOUR_OF_DAY, 0);
		temp.set(Calendar.MINUTE, 0);
		temp.set(Calendar.SECOND, 0);
		temp.set(Calendar.DAY_OF_MONTH, 1);
		return DateUtil.calendar2Str(temp, format);
	}
	
	
	
	/**
	 * 获得指定日期月的开始第一天
	 * @param time
	 * @param format
	 * @return
	 */
	public static String getCurrentTimeAfterTime(String time,String format) {
		Calendar temp = DateUtil.str2Calendar(time,format);
		temp.set(Calendar.HOUR_OF_DAY, 0);
		temp.set(Calendar.MINUTE, 0);
		temp.set(Calendar.SECOND, 0);
		temp.add(Calendar.MONTH, 1);
		temp.set(Calendar.DAY_OF_MONTH, 1);
		temp.set(Calendar.SECOND, -1);
		return DateUtil.calendar2Str(temp, format);
	}
	
	
	/**
	 * 获得指定日期月的最后一天
	 * @param time
	 * @param format
	 * @return
	 */
	public static String getLastDayOnMonth(String time ,String format) {
		Calendar temp = DateUtil.str2Calendar(time,format);
		temp.add(Calendar.MONTH, 1);
		return DateUtil.calendar2Str(temp, format);
	}
	
	public static void main(String[] args) {
		
	}
}
